package frsf.cidisi.faia.designer;

import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
    
public class EnvironmentDesignerGUI extends JFrame {
    
    private PanelDrawGUI panel;
    private javax.swing.JButton jButtonAceptar;
    private javax.swing.JButton jButtonSalir;
    private javax.swing.JList jListFiguras;
    private javax.swing.JList jListMateriales;
    private javax.swing.JPanel jPanelMaterial;
    private javax.swing.JPanel jPanelAceptar;
    private javax.swing.JPanel jPanelTipos;
    private javax.swing.JScrollPane jScrollFiguras;
    private javax.swing.JScrollPane jScrollMaterial;
    //private javax.swing.JPanel jPanelSensorData;
    private javax.swing.JLabel jLabelTipoFigura;
    private javax.swing.JLabel jLabelTipoMaterial;
    private javax.swing.JLabel jLabelSensorX;
    private javax.swing.JLabel jLabelSensorY;
    private javax.swing.JLabel jLabelAngle;
    private javax.swing.JLabel jLabelRange;
    private javax.swing.JLabel jLabelObject;
    private javax.swing.JLabel jLabelRangeData;
    private javax.swing.JLabel jLabelOjectData;
    private javax.swing.JLabel jLabelSensorXData;
    private javax.swing.JLabel jLabelSensorYData;
    private javax.swing.JLabel jLabelAngleData;
    private javax.swing.JLabel jLabelRebound;
    private javax.swing.JLabel jLabelReboundData;
    
    public EnvironmentDesignerGUI() {
        initComponents();
        initDos();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {
        
        panel = new PanelDrawGUI(this);
        panel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        
        jButtonSalir = new javax.swing.JButton();
        jButtonAceptar = new javax.swing.JButton();
        jPanelAceptar = new javax.swing.JPanel();
        
        jPanelTipos = new javax.swing.JPanel();
        
        jPanelMaterial = new javax.swing.JPanel();
        //jPanelSensorData = new javax.swing.JPanel();
        
        jScrollMaterial = new javax.swing.JScrollPane();
        jScrollFiguras = new javax.swing.JScrollPane();
        
        jListFiguras = new javax.swing.JList();
        jListMateriales = new javax.swing.JList();
        
        jLabelTipoFigura = new javax.swing.JLabel();
        jLabelTipoMaterial = new javax.swing.JLabel();
        jLabelSensorX = new javax.swing.JLabel();
        jLabelSensorY = new javax.swing.JLabel();
        jLabelAngle = new javax.swing.JLabel();
        jLabelRange = new javax.swing.JLabel();
        jLabelObject = new javax.swing.JLabel();
        jLabelRebound = new javax.swing.JLabel();
        
        jLabelSensorXData = new javax.swing.JLabel();
        jLabelSensorYData = new javax.swing.JLabel();
        jLabelAngleData = new javax.swing.JLabel();
        jLabelRangeData = new javax.swing.JLabel();
        jLabelOjectData = new javax.swing.JLabel();
        jLabelReboundData = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        
        jPanelTipos.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        
        jListFiguras.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Line", "Rectangle","Ellipse" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jListFiguras.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListFiguras.setToolTipText("");
 
        jListFiguras.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListFigurasValueChanged(evt);
            }
        });

        jPanelTipos.setBorder(javax.swing.BorderFactory.createEtchedBorder());


        jScrollFiguras.setViewportView(jListFiguras);

        jLabelTipoFigura.setText("Tipo de Figura");

        GroupLayout jPanelTiposLayout = new javax.swing.GroupLayout(jPanelTipos);
        jPanelTipos.setLayout(jPanelTiposLayout);
        jPanelTiposLayout.setHorizontalGroup(
            jPanelTiposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTiposLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTiposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollFiguras, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelTipoFigura))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelTiposLayout.setVerticalGroup(
            jPanelTiposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTiposLayout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jLabelTipoFigura)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollFiguras, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jPanelAceptar.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButtonSalir.setText("Salir");
        jButtonAceptar.setText("Aceptar");
        jButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAceptarActionPerformed(evt);
            }
        });

        GroupLayout jPanelAceptarLayout = new javax.swing.GroupLayout(jPanelAceptar);
        jPanelAceptar.setLayout(jPanelAceptarLayout);
        jPanelAceptarLayout.setHorizontalGroup(
            jPanelAceptarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAceptarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonSalir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonAceptar)
                .addContainerGap())
        );
        jPanelAceptarLayout.setVerticalGroup(
            jPanelAceptarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAceptarLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelAceptarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSalir)
                    .addComponent(jButtonAceptar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelMaterial.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jListMateriales.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Pared    (Rojo)", "Vidrio    (Azul)", "Tela      (Negro)", "Madera (Verde)", "Plastico (Cyan)" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jListMateriales.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListMateriales.setToolTipText("");
        jListMateriales.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListMaterialesValueChanged(evt);
            }
        });
        jScrollMaterial.setViewportView(jListMateriales);

        jLabelTipoMaterial.setText("Tipo de Material");

        GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanelMaterial);
        jPanelMaterial.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollMaterial, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelTipoMaterial))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabelTipoMaterial)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollMaterial, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );
        /*
        jPanelSensorData.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabelSensorX.setText("Sensor x:");
        jLabelSensorY.setText("Sensor y:");
        jLabelAngle.setText("Angle:");
        jLabelRange.setText("Range:");
        jLabelObject.setText("Object:");
        jLabelRebound.setText("Rebound:");
        jLabelSensorYData.setText("");
        jLabelSensorXData.setText("");
        jLabelAngleData.setText("");
        jLabelRangeData.setText("");
        jLabelOjectData.setText("");
        jLabelReboundData.setText("");

        GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanelSensorData);
        jPanelSensorData.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelRebound)
                    .addComponent(jLabelAngle)
                    .addComponent(jLabelSensorY)
                    .addComponent(jLabelSensorX)
                    .addComponent(jLabelRange)
                    .addComponent(jLabelObject))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelSensorYData, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabelAngleData, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabelSensorXData, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                            .addComponent(jLabelReboundData, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelOjectData, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelRangeData, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(42, 42, 42))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelSensorX)
                    .addComponent(jLabelSensorXData, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelSensorY)
                    .addComponent(jLabelSensorYData))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelAngle)
                    .addComponent(jLabelAngleData))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelRange)
                    .addComponent(jLabelRangeData))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelObject)
                    .addComponent(jLabelOjectData))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelRebound)
                    .addComponent(jLabelReboundData))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        */
        
        
        GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, 640, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanelAceptar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelTipos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelMaterial, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))   
                    //.addComponent(jPanelSensorData, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                 );
        
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(22, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanelTipos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanelMaterial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        //.addComponent(jPanelSensorData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        //.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanelAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, 480, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        this.setSize(860, 550);
        this.setResizable(false);
    
    }                        

    private void initDos() {
       
       panel.setMaterial(1);
       panel.setForm(1);
       jListMateriales.setSelectedIndex(0);
       jListFiguras.setSelectedIndex(0);
       //actualizarComponentes();
    }
    
    private void jButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {                                               
        //rellenarDibujo();
    }                                              

    private void jListFigurasValueChanged(javax.swing.event.ListSelectionEvent evt) {                                          

        panel.setX1(0);
        panel.setX2(0);
        panel.setY1(0);
        panel.setY2(0);
        panel.setWidth(0);
        panel.setHigh(0); 
        panel.setForm(jListFiguras.getSelectedIndex()+1);

    }                                         

    private void jListMaterialesValueChanged(javax.swing.event.ListSelectionEvent evt) {                                             
        
        panel.setX1(0);
        panel.setX2(0);
        panel.setY1(0);
        panel.setY2(0);
        panel.setWidth(0);
        panel.setHigh(0); 
        panel.setMaterial(jListMateriales.getSelectedIndex()+1);        
        
    }                                            

    public int getMaterialSeleccionado(){
        return jListFiguras.getSelectedIndex()+1;
    }
    
    /*
    public void actualizarComponentes() {
        this.jLabelX.setText(Double.toString(canvas1.getSensor().getX1()));
        this.jLabelY.setText(Double.toString(canvas1.getSensor().getY1()));
        this.jLabelangulo.setText(Float.toString((float)canvas1.getSensor().getAnguloGrado()));
        double[] resp = canvas1.sensor.distanciaMinima(); 
        double a  = resp[0];
        double b = resp[1];
        
        this.jLabelDistancia.setText(Float.toString((float)a));
        if(b==1000){
            this.jLabelOjeto.setText("ninguno");
            this.jLabelChoque.setText("ninguno");
        }
        else{
            this.jLabelOjeto.setText(Double.toString(b));

            Class clase = canvas1.listaObjetos.get((int)b).getClass();
            
            if(clase.isInstance(new java.awt.geom.Line2D.Double())){
                Line2D.Double l = (Line2D.Double) canvas1.listaObjetos.get((int)b);     
                double res = canvas1.getSensor().ChoqueLinea(l);
                this.jLabelChoque.setText((Float.toString((float)res)));
            }
            
            if(clase.isInstance(new java.awt.geom.Ellipse2D.Double())){
                Ellipse2D.Double l = (Ellipse2D.Double) canvas1.listaObjetos.get((int)b);  
                double res = canvas1.getSensor().ChoqueEsfera(l);            
                this.jLabelChoque.setText((Float.toString((float)res)));
            }
        }   
    }
    */
    
    public static void main(String args[]) {
        
        try{  
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) 
            {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EnvironmentDesignerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EnvironmentDesignerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EnvironmentDesignerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EnvironmentDesignerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new EnvironmentDesignerGUI().setVisible(true);
            }
        });
    }
}